import { Slides } from 'ionic-angular';
import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { UploadPhotoService } from '../../services/upload-photo.service';
import { Attachment } from '../../models/transform/attachment';

@Component({
  selector: 'bigpic-preview-sliders',
  templateUrl: 'bigpic-preview-sliders.component.html'
})
export class BigpicPreviewSlidersComponent  {
  @Input() attachments: Array<any>;
  @ViewChild(Slides) slides: Slides;

   showBigPic: boolean = false;
   startByIndex: number = 0;
   loadingPic: string = '/assets/img/loading.gif';

  // constructor(private sanitizer: DomSanitizer,
  //             private imageService: UploadPhotoService) {
  // }

  ngOnInit(): void { console.log(); }

  public slideChanged() {
    const currentIndex = this.slides.getActiveIndex();
    // const currentIndex=1;
    const attachment = this.attachments[currentIndex];
    this.slides.lockSwipes(true);

    this.attachments[currentIndex] = attachment;
    this.slides.lockSwipes(false);
    // if (!attachment) {
    //   return;
    // }
    // if (attachment.bigPic && attachment.bigPic.startsWith('data:')) {
    //   return;
    // }
    
    // // download file
    // this.imageService.getImage(attachment.bigPicId).then(((imageData) => {
    //   if (imageData) {
    //     attachment.bigPic = imageData;
    //   }
    //   this.attachmentArray[currentIndex] = attachment;
    //   this.slides.lockSwipes(false);
    // }));
  }

  public updateAttachmentArray(newArray: Array<Attachment>) {
    this.attachments = newArray || [];
  }

  public preview(previewIndex) {
    if (previewIndex < 0) {
      return;
    }
    this.showBigPic = true;
    this.startByIndex = previewIndex;
    const currentAttachment = this.attachments[previewIndex];
    this.attachments[previewIndex] = currentAttachment;

    // const currentAttachment = this.attachmentArray[previewIndex];
    // if (currentAttachment.bigPic && currentAttachment.bigPic.startsWith('data:')) {
    //   return;
    // }
    // currentAttachment.bigPic = this.loadingPic;
    // this.imageService.getImage(currentAttachment.bigPicId).then((imageData) => {
    //   if (imageData) {
    //     this.attachmentArray[previewIndex].bigPic = imageData;
    //   }
    // });
  }

  public closePreview() {
    this.showBigPic = false;
  }


  public isShow() {
    return this.showBigPic;
  }
}
